package com.lianjia.sh.se.esb.client.sender.impl;

import com.alibaba.fastjson.JSONObject;
import com.lianjia.sh.se.esb.client.sender.MessageSender;
import com.lianjia.sh.se.esb.client.service.impl.ActiveMqMessageService;
import com.lianjia.sh.se.esb.client.service.MQMessageService;
import com.lianjia.sh.se.esb.spi.model.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 消息发送服务
 * @author yangjiangping
 * @since 2016 -07-15 16:03
 */
@Service
public class SimpleMessageSender implements MessageSender{
    Logger logger = LoggerFactory.getLogger(ActiveMqMessageService.class);
    @Autowired
    MQMessageService mqMessageService;

    /**
     * 异步发送消息
     *
     * 业务与调用发送操作异步
     *  @param message 消息
     * @author yangjiangping
     * @since 2016 -07-15 16:03
     */
    @Async
    public void sendMQMsgAsync(Message message){
       sendApiMsg(message);
    }

    /**
     * 同步发送消息
     *
     * 业务与调用发送操作同步，业务与消息消费者后续操作仍然为异步（因为使用mq）
     *
     * @param message 消息
     * @author yangjiangping
     * @since 2016 -07-15 16:03
     */
    public void sendMQMsg(Message message){
        String msgJson = JSONObject.toJSONString(message);
        try {
            mqMessageService.sendMessage(message.getProjectType(),message.getServiceType(),msgJson);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 同步发送消息
     *
     * 直接通过api调用发送，全程同步，立即生效
     *
     * @param message 消息
     * @author yangjiangping
     * @since 2016 -07-15 16:03
     */
    public void sendApiMsg(Message message){

    }

    @Override
    public void sendKafkaMsg(Message message) {

    }
}
